/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.states;

import PacketHandling.PacketHandler;
import horizon.entity.Entity;
import horizon.entity.Player;
import horizon.graphics.RenderingContext;
import horizon.gui.PlanetGui;
import horizon.gui.component.GuiWindow;
import horizon.map.Map;
import horizon.util.ExpandedColor;
import horizon.util.InputManager;
import horizon.util.Vector2;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
 *
 * @author J_Laptop
 */
public class GameState extends State {

    public Map m;
    String name = "";
    long time = 0;
    long packetWait = 100;
    PlanetGui gui;
    boolean invOpen = false;

    public GameState(Map scene, String s) {
        super(s);
        this.m = scene;
        name = PacketHandler.getInstance().getName();


    }

    @Override
    public void init() {
        gui = new PlanetGui(this);
    }

    @Override
    public void render(RenderingContext r) {
        r.setCamPos(((Player) m.getEntity(name)).position);
        m.render(r);
        for (Entity e : m.getEntities().values()) {

            if (e instanceof Player) {

                r.drawPlayerLabel(e.getId(), r.WorldToScreenCoordinates(e.getPosition().add(new Vector2(32, 0))), Color.white, ExpandedColor.transGray);

            }

        }
        gui.render(r);

    }

    @Override
    public void update(long elapsedTime) {
        ArrayList<Entity> entities = PacketHandler.getInstance().getAvailableEntities();
        for (Entity e : entities) {

            m.addEntity(e);

        }

        if (InputManager.getInstance().isKeyDown(KeyEvent.VK_W)) {
            movePlayer("forward");

        } else if (InputManager.getInstance().isKeyDown(KeyEvent.VK_A)) {
            movePlayer("left");
        } else if (InputManager.getInstance().isKeyDown(KeyEvent.VK_S)) {
            movePlayer("back");
        } else if (InputManager.getInstance().isKeyDown(KeyEvent.VK_D)) {
            movePlayer("right");
        } else {
            movePlayer("none");
        }

        if (InputManager.getInstance().isKeyDown(KeyEvent.VK_Q) && !invOpen) {

            GuiWindow window = new GuiWindow("Inventory", new Vector2(200, 200), new Vector2(300, 200), Color.BLUE, Color.black);

            gui.addComponent(window);
            invOpen = true;

        } else if (InputManager.getInstance().isKeyDown(KeyEvent.VK_Q) && invOpen) {

            gui.removeComponent("Inventory");
            invOpen = false;

        }





        for (Entity e : m.getEntities().values()) {

            if (e instanceof Player && !e.getId().equals(PacketHandler.getInstance().getName())) {

                Player p = (Player) e;
                p.move(p.getLastMov());


            }

        }

        m.update(elapsedTime);


        if (time > packetWait) {
            PacketHandler.getInstance().sendCurrentRequest();
            time = 0;
        } else {

            time += elapsedTime;

        }

        gui.update(elapsedTime);

    }

    @Override
    public void actionGui(String s) {
    }

    private void movePlayer(String s) {

        Vector2 v = ((Player) m.getEntity(name)).movePlayer(s);

        PacketHandler.getInstance().addMovementUpdate(m.getEntity(name).getPosition(), v);


    }

    public void addChatLine(String s) {

        gui.addMessageToChat(s);

    }
}
